package com.xiaohei.process.controller.api;

import ch.qos.logback.core.pattern.util.RegularEscapeUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaohei.auth.service.SysUserService;
import com.xiaohei.common.result.Result;
import com.xiaohei.model.process.Process;
import com.xiaohei.model.process.ProcessTemplate;
import com.xiaohei.model.process.ProcessType;
import com.xiaohei.process.service.ProcessService;
import com.xiaohei.process.service.ProcessTemplateService;
import com.xiaohei.process.service.ProcessTypeService;
import com.xiaohei.vo.process.ApprovalVo;
import com.xiaohei.vo.process.ProcessFormVo;
import com.xiaohei.vo.process.ProcessVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * ClassName: ProcessApiController
 * Package:
 * Description:
 *
 * @Author:xiaohei
 * @Create 2024/5/6 10:38
 * Version 1.0
 */
@Api(tags = "审批流管理")
@RestController
@RequestMapping(value="/admin/process")
@CrossOrigin  //跨域
public class ProcessApiController {

    @Autowired
    private ProcessTypeService processTypeService;
    @Autowired
    private ProcessTemplateService processTemplateService;
    @Autowired
    private ProcessService processService;
    @Autowired
    private SysUserService sysUserService;

    //查询所有分类和每个分类对应的审批模版
    @GetMapping("findProcessType")
    public Result findProcessType(){
        List<ProcessType> list = processTypeService.getProcessType();
        return Result.ok(list);
    }

    //根据id获取模版信息
    @ApiOperation("根据模版id获取模版信息")
    @GetMapping("getProcessTemplate/{processTemplateId}")
    public Result get(@PathVariable Long processTemplateId){
        ProcessTemplate processTemplate = processTemplateService.getById(processTemplateId);
        return Result.ok(processTemplate);
    }

    @ApiOperation(value = "启动流程")
    @PostMapping("/startUp")
    public Result startUp(@RequestBody ProcessFormVo processFormVo){
        processService.startUp(processFormVo);
        return Result.ok();
    }

    @ApiOperation(value = "待处理")
    @GetMapping("/findPending/{page}/{limit}")
    public Result findPending(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit){
        Page<Process> pageParam = new Page<>(page,limit);
        IPage<ProcessVo> pageModel = processService.findPending(pageParam);
        return Result.ok(pageModel);
    }

    //查看审批详情信息
    @GetMapping("show/{id}")
    public Result show(@PathVariable Long id){
        Map<String,Object> map = processService.show(id);
        return Result.ok(map);
    }


    //审批
    @ApiOperation(value = "审批")
    @PostMapping("approve")
    public Result approve(@RequestBody ApprovalVo approvalVo){
        processService.approve(approvalVo);
        return Result.ok();
    }

    @ApiOperation(value = "已处理")
    @GetMapping("/findProcessed/{page}/{limit}")
    public Result findProcessed(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit){
        Page<ProcessVo> pageParam = new Page<>(page,limit);
        IPage<ProcessVo> pageModel = processService.findProcess(pageParam);
        return Result.ok(pageModel);
    }

    @ApiOperation(value = "已发起")
    @GetMapping("/findStarted/{page}/{limit}")
    public Result findStarted(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,

            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit) {
        Page<ProcessVo> pageParam = new Page<>(page, limit);
        IPage<ProcessVo> pageModel = processService.findStarted(pageParam);
        return Result.ok(pageModel);
    }

    //获取当前用户
    @ApiOperation(("获取当前用户"))
    @GetMapping("getCurrentUser")
    public Result getCurrentUser(){
        Map<String,Object> map = sysUserService.getCurrentUser();
        return Result.ok(map);
    }

}
